﻿@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Authorization

@using Covenant.Models.Covenant
@using Covenant.Models.Listeners
@using Covenant.Components.Shared
@inject IJSRuntime IJSRuntime
@inject AuthenticationStateProvider AuthenticationStateProvider

<EditForm Model="Profile" OnValidSubmit="(e => this.OnSubmit.InvokeAsync(this.Profile))">
    <DataAnnotationsValidator />
    <div class="form-row">
        <div class="form-group col-md-4">
            <label for="Name">Name</label>
            <input id="Name" name="Name" @bind="Profile.Name" class="form-control">
            <div class="text-danger"><ValidationMessage For="() => Profile.Name" /></div>
        </div>
        <div class="form-group">
            <label for="Type">Type</label><br />
            <EnumSelect id="Type" name="Type" @bind-Value="Profile.Type" disabled="true" />
            <div class="text-danger"><ValidationMessage For="() => Profile.Type" /></div>
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-12">
            <label for="Description">Description</label>
            <input id="Description" name="Description" @bind="Profile.Description" class="form-control">
            <div class="text-danger"><ValidationMessage For="() => Profile.Description" /></div>
        </div>
    </div>
    <EditFormList @bind-FormList="Profile.HttpUrls" InputName="HttpUrls"></EditFormList>
    <div class="text-danger"><ValidationMessage For="() => Profile.HttpUrls" /></div>
    @if (IsAdmin())
    {
        <div class="form-row">
            <div class="form-group col-md-12">
                <label for="MessageTransform">MessageTransform</label>
                <textarea @ref="CodeMirrorElements[0]" id="MessageTransform" name="MessageTransform" @bind="Profile.MessageTransform" rows="10" class="form-control code-mirror-csharp"></textarea>
                <div class="text-danger"><ValidationMessage For="() => Profile.MessageTransform" /></div>
            </div>
        </div>
    }
    <EditHttpProfileHeaderList @bind-FormList="Profile.HttpRequestHeaders" InputName="HttpRequestHeaders"></EditHttpProfileHeaderList>
    <div class="text-danger"><ValidationMessage For="() => Profile.HttpRequestHeaders" /></div>
    <EditHttpProfileHeaderList @bind-FormList="Profile.HttpResponseHeaders" InputName="HttpResponseHeaders"></EditHttpProfileHeaderList>
    <div class="text-danger"><ValidationMessage For="() => Profile.HttpResponseHeaders" /></div>
    <div class="form-row">
        <div class="form-group col-md-12">
            <label for="HttpPostRequest">HttpPostRequest</label>
            <textarea @ref="CodeMirrorElements[1]" id="HttpPostRequest" name="HttpPostRequest" @bind="Profile.HttpPostRequest" rows="20" class="form-control code-mirror-html"></textarea>
            <div class="text-danger"><ValidationMessage For="() => Profile.HttpPostRequest" /></div>
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-12">
            <label for="HttpGetResponse">HttpGetResponse</label>
            <textarea @ref="CodeMirrorElements[2]" id="HttpGetResponse" name="HttpGetResponse" @bind="Profile.HttpGetResponse" rows="20" class="form-control code-mirror-html"></textarea>
            <div class="text-danger"><ValidationMessage For="() => Profile.HttpGetResponse" /></div>
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-12">
            <label for="HttpPostResponse">HttpPostResponse</label>
            <textarea @ref="CodeMirrorElements[3]" id="HttpPostResponse" name="HttpPostResponse" @bind="Profile.HttpPostResponse" rows="20" class="form-control code-mirror-html"></textarea>
            <div class="text-danger"><ValidationMessage For="() => Profile.HttpPostResponse" /></div>
        </div>
    </div>

    <button type="submit" class="btn btn-primary">
        <span class="fe fe-@SubmitIcon"></span> @SubmitLabel
    </button>
</EditForm>

@code {
    [Parameter]
    public HttpProfile Profile { get; set; }

    [Parameter]
    public EventCallback<HttpProfile> OnSubmit { get; set; }

    [Parameter]
    public string SubmitIcon { get; set; } = "edit";

    [Parameter]
    public string SubmitLabel { get; set; } = "Edit";

    [CascadingParameter]
    public Theme Theme { get; set; }

    private IList<ElementReference> CodeMirrorElements { get; set; } = GetElementReferenceList(4);

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            foreach (ElementReference CodeMirrorElement in CodeMirrorElements)
            {
                await IJSRuntime.InvokeAsync<string>("InitializeCodeMirror", CodeMirrorElement, Theme.CodeMirrorTheme.ToString());
            }
        }
    }

    private static IList<ElementReference> GetElementReferenceList(int size)
    {
        IList<ElementReference> references = new List<ElementReference>();
        for (int i = 0; i < size; i++)
        {
            references.Add(new ElementReference());
        }
        return references;
    }

    private IList<KeyValuePair<string, string>> ToKeyValueList(IList<HttpProfileHeader> headers)
    {
        return headers.Select(H => new KeyValuePair<string, string>(H.Name, H.Value)).ToList();
    }

    private bool IsAdmin()
    {
        Task<AuthenticationState> t = AuthenticationStateProvider.GetAuthenticationStateAsync();
        t.Wait();
        return t.Result.User.IsInRole("Administrator");
    }
}
